package online.heycm.awss3.aaa;

import com.amazonaws.services.s3.model.AccessControlList;
import com.amazonaws.services.s3.model.Grant;
import lombok.extern.slf4j.Slf4j;
import online.heycm.platform.oss.OssService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;

@Slf4j
@RestController
public class Abc {

    @Autowired
    private OssService ossService;

    @GetMapping("/aaa")
    public List<Grant> aaa() {
        AccessControlList bucketAcl = ossService.getBucketAcl();
        List<Grant> grantsAsList = bucketAcl.getGrantsAsList();
        return grantsAsList;
    }

    @PostMapping("/bbb")
    public String bbb(@RequestParam("file") MultipartFile file,
                      @RequestParam(value = "objectName", required = false) String objectName) throws IOException {
        objectName = objectName == null ? file.getOriginalFilename() : objectName;
        boolean b = ossService.putObject(objectName, file.getInputStream(), file.getContentType(), null);
        System.out.println("b = " + b);
        return ossService.getUrl(objectName);
    }

    @GetMapping("/ccc")
    public String ccc(@RequestParam("objectName") String objectName) {
        return ossService.getPresignedUrl(objectName);
    }

    @GetMapping("/ddd")
    public String ddd(@RequestParam("objectName") String objectName) {
        return ossService.getPutUrl(objectName);
    }

    @GetMapping("/eee")
    public String eee(@RequestParam("objectName") String objectName) {
        ossService.deleteObject(objectName);
        return "ok";
    }

    @GetMapping("/fff")
    public void fff(@RequestParam("objectName") String objectName, HttpServletResponse response) throws IOException {
        try (InputStream s3InputStream = ossService.getObject(objectName).getObjectContent()) {
            response.setContentType("application/octet-stream");
            String s = objectName.replaceAll("/", "_");
            s = URLEncoder.encode(s, "UTF-8");
            response.setHeader("Content-Disposition", "attachment; filename*=UTF-8''" + s);

            try (OutputStream outputStream = response.getOutputStream()) {
                byte[] buffer = new byte[4096];
                int bytesRead;
                while ((bytesRead = s3InputStream.read(buffer)) != -1) {
                    outputStream.write(buffer, 0, bytesRead);
                }
            }

            response.setStatus(HttpServletResponse.SC_OK);
            log.info("Successfully sent S3 object with name: {}", objectName);
        } catch (IOException e) {
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            log.error("Failed to send S3 object with name: {}", objectName, e);
        }
    }

    @GetMapping("/ggg")
    public List<String> listObject(@RequestParam("objectName") String objectName, @RequestParam("targetName") String targetName) {
        ossService.copyObject(objectName, targetName);
        return ossService.listObject();
    }

    @GetMapping("/123")
    public List<String> listObject() {
        return ossService.listObject();
    }

    @DeleteMapping("/hhh")
    public List<String> del(@RequestParam("objectName") String objectName) {
        boolean b = ossService.deleteObject(objectName);
        System.out.println(b);
        return ossService.listObject();
    }

    @GetMapping("/iii")
    public List<String> iii(@RequestParam("dir") String dir) {
        ossService.uploadDirectory(dir, new File("C:/Users/何宇/Desktop/配图"));
        return ossService.listObject();
    }
}
